require "findPath"
require "helper"
require "gui"
require "guiFunctions"

angleText = 0
static = {}
terrain = {}
particles = {}
waypoints = {}
finalPoints = {}
chosenPath = {}
currentPoints = {}
highlightPath = {}
successfulPoints = {}
lights = {}
particles = {}
intensity = 100
selectedRobot = {}
kMatrix = {}
tempEnd = {}
wayDist = 0
finDist = 0

function saveEnd(x,y)
	tempEnd[0] = x
	tempEnd[1] = y
end

function love.load()
    -- Fat lines.
    love.graphics.setLineWidth(2)
	love.graphics.setMode( 800, 700, false)
	
    -- Load images.
    images = {
        explode = love.graphics.newImage("explode1.png")
    }
    world = love.physics.newWorld(0,0,2000, 2000)
	gui = createGUIcontroller() -- gui is the name of the guiController
	createGUI(gui) --create all the textboxes and buttons
end

function createGUI(ctrl)
	createTextbox(ctrl,60,600,40,20,"Start X",40)
	createTextbox(ctrl,60,625,40,20,"Start Y",40)
	
	createTextbox(ctrl,160,600,40,20,"End X",600)
	createTextbox(ctrl,160,625,40,20,"End Y",100)

	createButton(ctrl,320,600,20,"Clear Path", clearPath)
	createButton(ctrl,320,625,20,"Clear Squares", clearSquares)
	createButton(ctrl,320,650,20,"Draw Path", drawPath)

end

function createWayPoint(x, y, nextX, nextY)
	local t = {}
	t.x = x;        t.y = y
	t.angle = 0
	
	t.ox = 16; 	    t.oy = 16
	t.children = -1
	t.info = '';    t.index = 1
	
    t.nextX = nextX
    t.nextY = nextY
	
	table.insert(waypoints,t)
	t.index = #waypoints
	
	return t
end

function createSquare(x,y,w,h)
	local t = {}
	t.type = 'square'
	t.x = x
	t.y = y
	t.x2 = t.x+w
	t.y2 = t.y+h	
	
	t.h = h	
	t.w = w	
	table.insert(terrain,t)
end

function love.update(dt)
	--gui object handling is in other files
	handleTextBoxes(gui)
	handleButtons(gui)
	
	if love.keyboard.isDown('escape') then
        love.event.push('q') -- quit the game
    end
	
	for i,v in ipairs(particles) do
		v:update(dt)
	end
end

function love.draw()
	if endPoint ~= nil then
		local currentPoint = endPoint
		love.graphics.setColor(100,100,255)
		
		while currentPoint.parent ~= nil and currentPoint.parent.x ~= nil do
			-- love.graphics.draw(currentPoint.i, currentPoint.x, currentPoint.y, 0, 1, 1, currentPoint.ox, currentPoint.oy)
			love.graphics.line(currentPoint.x,currentPoint.y,currentPoint.parent.x,currentPoint.parent.y)
			-- love.graphics.print(currentPoint.info,currentPoint.x + 20,currentPoint.y,0,1,1)
			currentPoint = currentPoint.parent
		end
		love.graphics.setColor(255,255,255)
	end
    
	for i,v in ipairs(terrain) do
		love.graphics.rectangle("fill",v.x, v.y, v.w, v.h)
    end
    
	---[[
	love.graphics.setColor(255,255,0)
	for i,v in ipairs(waypoints) do
		if v.nextX ~= nil then
			love.graphics.line(v.x, v.y, v.nextX, v.nextY)
		else
			love.graphics.print(wayDist, v.x, v.y+15)
		end
	end
	--]]
	love.graphics.setColor(0,255,255)
	for i,v in ipairs(finalPoints) do
		if v.nextX ~= nil then
			love.graphics.line(v.x, v.y, v.nextX, v.nextY)
		else
			love.graphics.print(finDist, v.x, v.y)
		end
	end
	
	
	
	if dragx1 ~= -1 and dragy1 ~= -1 then
		love.graphics.rectangle("line", dragx1, dragy1, love.mouse.getX()-dragx1,love.mouse.getY()-dragy1)
	end	
	
	drawGUI(gui)
end
